package com.bignerdranch.android.ecar.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.android.volley.Response;
import com.bignerdranch.android.ecar.Adapter.UsedCarAdapter;
import com.bignerdranch.android.ecar.R;
import com.bignerdranch.android.ecar.Utils.CheckNet;
import com.bignerdranch.android.ecar.Utils.RequestUtil;
import com.bignerdranch.android.ecar.Utils.URLBank;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;

public class UsedCarActivity extends BaseActivity implements View.OnClickListener {

    private ImageButton used_car_back;
    private RelativeLayout sort;
    private RelativeLayout carBrand;
    private RelativeLayout car_price;
    private ListView used_car_list;
    private JSONArray mData = new JSONArray();
    private UsedCarAdapter mAdapter;
    JSONObject object;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_used_car);
        initView();
        if (CheckNet.getNet(this))
        initData();



        used_car_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                try {
                    String car_id = mData.getJSONObject(position).getString("id");
                    Intent intent = new Intent(getApplicationContext(),UsedCarDetailActivity.class);
                    intent.putExtra("id",car_id);
                    startActivity(intent);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void initView() {
        used_car_back = (ImageButton) findViewById(R.id.used_car_back);
        sort = (RelativeLayout) findViewById(R.id.sort);
        sort.setOnClickListener(this);
        carBrand = (RelativeLayout) findViewById(R.id.carBrand);
        carBrand.setOnClickListener(this);
        car_price = (RelativeLayout) findViewById(R.id.car_price);
        car_price.setOnClickListener(this);
        used_car_list = (ListView) findViewById(R.id.used_car_list);
        used_car_back.setOnClickListener(this);



    }

    private void initData(){
        String url = URLBank.teacher_url+URLBank.queryPage_url;

        RequestUtil.execute2(this, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    object = new JSONObject(s);
                    mData = object.getJSONArray("data");
                    mAdapter = new UsedCarAdapter(getApplicationContext(),mData);
                    used_car_list.setAdapter(mAdapter);
                    mAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },null);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.used_car_back:
                finish();
                break;
            case R.id.sort:

                break;
            case R.id.carBrand:

                break;
            case R.id.car_price:

                break;
        }
    }
}
